/**
 * 维修收入结算维护 - 编辑页面
 * @author sxc
 * @date 2019/01/18
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            isAdd: false,
            isCheck: false,
            form: {
                MMRXPLANT_NAME: null,
                MMRXLEGAL_NAME: null,
                MMRX018_NAME: null,
                MMRX019_NAME: null,
                MMRXUSER: null,
                MMRXGRUP: null,
                MMRXORIG: null,
                MMRXORIU: null,
                MMRX022_NAME: null,
                MMRX032_NAME: null,
                MMRX024_DATA: null,
                MMRX025_DATA: null,
                MMRX026_DATA: null,
            },
            GWAK057: '', //汇率类型
            MMRX025_GWAL018: '',
            MMRX025_GWAL021: '',
            MMRX025_GWAL020: '',
            MMRX026_GWAL018: '',
            MMRX026_GWAL021: '',
            MMRX026_GWAL020: '',
            MMRX005_SUM: 0,
            GWEA006: '', //税率
            MMRX010: '', //折扣率
            MMRX009: '', //汇率
            MMRX006: '', //币种
            MMRX006_NAME: '', //币种名称
            MMRX007: '', //税种
            MMRX007_NAME: '', //税种名称
            AMOUNT: 10,
            rules: {
                GWCSDOCNO: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWCSDOCDATE: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWCS010: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWCS117: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWCS118: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]
            }
        },
        cols = [[
            { field: 'MMRXSEQ', type: 'checkbox', title: '选择'}
            , {field: 'MMRX001', width: 160,title: '配件编号', event: 'openGwdc001' }
            , {field: 'GWAML003',width: 160,title: '品名',
                templet: function (data) {
                    if(data.GWAML003 != undefined){
                        return data.GWAML003;
                    }else if(data.MMRCL003 != undefined){
                        return data.MMRCL003;
                    }else {
                        return '';
                    }
                }
            }
            , {field: 'GWAML004',width: 160,title: '规格'}
            , {field: 'MMRX002',width: 80,title: '单位'}
            , {field: 'MMRX003', width: 80,title: '数量', edit: 'text'}
            , {field: 'MMRX007_NAME', width: 120,title: '税种', event: 'openMmrx007'}
            , {field: 'MMRX006', width: 80,title: '币别'}
            , {field: 'MMRX009', width: 80,title: '汇率', edit: 'text'}
            , {field: 'MMRX004', width: 80,title: '原币单价', edit: 'text'}
            , {field: 'MMRX005', width: 120,title: '原币含税金额', edit: 'text'}
            , {field: 'MMRX011', width: 120,title: '原币含税折扣价', edit: 'text'}
            , {field: 'MMRX015', width: 80,title: '原币税额', edit: 'text'}
            , {field: 'MMRX012', width: 80,title: '本币单价', edit: 'text'}
            , {field: 'MMRX013', width: 120,title: '本币含税金额', edit: 'text'}
            , {field: 'MMRX014', width: 120,title: '本币含税折扣价', edit: 'text'}
            , {field: 'MMRX016', width: 80,title: '本币税额', edit: 'text'}
            , {field: 'MMRX031', width: 80,title: '备注', edit: 'text'}
            , {field: 'MMRX007', width: 80,hide: true}
            , {field: 'MMRX027', width: 80,hide: true}
            , {field: 'MMRX028', width: 80,hide: true}
            , {field: 'GWEA006', width: 80,hide: true}
            , {field: 'MMRX010', width: 80,hide: true}
        ]],
        callObj,
        moduleName,
        listData = null,
        table, gwei025Table, layOpenIndex
    ;

    var Gwei025Edit = app.Module.extend({
            init: function () {
                self = this;
                self._super({
                    _tag: 'gwei025_edit',
                    _loadTplsFull: [{
                        dom: 'common_open_multi_mmrghi', tpl: 'common/_open_multi_mmrghi' //0
                    },{
                        dom: 'common_open_window', tpl: 'common/_open_window' //1
                    },{
                        dom: 'gwei025_edit_gwak', tpl: '/gwei025_edit_gwak' //2
                    },{
                        dom: 'gwei025_edit_gwal', tpl: '/gwei025_edit_gwal' //3
                    }
                    ]
                }, win);
            },
            
            initTable: function () {
                layui.use(['table'], function () {
                    table = layui.table;
                    gwei025Table = table.render({
                        elem: app.getByid(doc, 'gwei025_edit_table'),
                        cellMinWidth: 80,
                        id: 'gwei025_edit_table',
                        defaultToolbar:false,
                        cols: cols,
                        data: listData,
                        page: false
                    });

                    table.on('tool(gwei025_edit_table)', function (obj) {
                        if(obj.event == 'openMmrx007'){
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_gwea001',
                                        columnList: [
                                            {label: '税种编号', prop: 'GWEA001', width: '150'},
                                            {label: '税种名称', prop: 'GWEA002', width: '150'},
                                            {label: '税率', prop: 'GWEA006', width: '150'}
                                        ],
                                        //url: 'crm/gwei025/getGweaData'
                                    },
                                    cb: function (data) {
                                        obj.updateOfEdit({
                                            MMRX007_NAME: data.GWEA002,
                                            MMRX007: data.GWEA001,
                                            GWEA006: data.GWEA006,
                                            MMRX015: obj.data.MMRX011 != '' && obj.data.MMRX011 != undefined ?
                                                self.calculate(math.div(math.mul(obj.data.MMRX011, math.div(data.GWEA006,100)), math.add(1, math.div(obj.data.GWEA006,100))), vueData.AMOUNT) : '',
                                            MMRX016: obj.data.MMRX014 != '' && obj.data.MMRX014 != undefined ?
                                                self.calculate(math.div(math.mul(obj.data.MMRX014, math.div(data.GWEA006,100)), math.add(1, math.div(obj.data.GWEA006,100))), vueData.AMOUNT) : ''
                                        })
                                    }
                                },
                                index: 1,
                                openOpts: {
                                    title: '选择税种信息',
                                    area: ['600px', '470px']
                                }
                            });
                        }
                    });

                    //监听单元格编辑
                    table.on('edit(gwei025_edit_table)', function (obj) {
                        if (obj.field == 'MMRX003') {//数量
                            var parnt = /^([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])$/;
                            if(!parnt.test(obj.value)){
                                app.showWarning("数量必须大于0！");
                                return;
                            }
                            if(obj.data.MMRX004 != '' && obj.data.MMRX004 != null){
                                var MMRX005 = self.calculate(math.mul(obj.data.MMRX004, obj.value), vueData.AMOUNT);
                                var MMRX011 = self.calculate(math.mul(MMRX005, obj.data.MMRX010), vueData.AMOUNT);
                                var MMRX013 = obj.data.MMRX009 != '' ? self.calculate(math.mul(MMRX005, obj.data.MMRX009), vueData.AMOUNT) : '';
                                var MMRX014 = obj.data.MMRX009 != '' ? self.calculate(math.mul(MMRX013, obj.data.MMRX010), vueData.AMOUNT) : '';
                                var MMRX015 = self.calculate(math.div(math.mul(MMRX011, math.div(obj.data.GWEA006,100)), math.add(1, math.div(obj.data.GWEA006,100))), vueData.AMOUNT);
                                var MMRX016 = self.calculate(math.div(math.mul(MMRX014, math.div(obj.data.GWEA006,100)), math.add(1, math.div(obj.data.GWEA006,100))), vueData.AMOUNT);
                                obj.updateOfEdit({
                                    MMRX005: MMRX005,
                                    MMRX011: MMRX011,
                                    MMRX013: MMRX013,
                                    MMRX014: MMRX014,
                                    MMRX015: MMRX015,
                                    MMRX016: MMRX016
                                });
                                var listData = gwei025Table.getDatas();
                                var sum = 0;
                                for(var i=0;i<listData.length;i++){
                                    sum = math.add(sum, listData[i].MMRX005);
                                }
                                vueData.MMRX005_SUM = sum;
                            }
                        }else if(obj.field == 'MMRX004'){//单价
                            var MMRX012 = obj.data.MMRX009 != '' ? self.calculate(math.mul(obj.value, obj.data.MMRX009), vueData.AMOUNT) : '';
                            var MMRX005 = obj.data.MMRX003 != '' ? self.calculate(math.mul(obj.data.MMRX003, obj.value), vueData.AMOUNT) : '';
                            var MMRX011 = MMRX005 != '' ? self.calculate(math.mul(MMRX005, obj.data.MMRX010), vueData.AMOUNT) : '';
                            var MMRX013 = obj.data.MMRX009 != '' && obj.data.MMRX003 != '' ? self.calculate(math.mul(MMRX012, obj.data.MMRX003), vueData.AMOUNT) : '';
                            var MMRX014 = obj.data.MMRX009 != '' && MMRX013 != '' ? self.calculate(math.mul(MMRX013, obj.data.MMRX010), vueData.AMOUNT) : '';
                            var MMRX015 = MMRX011 != '' ? self.calculate(math.div(math.mul(MMRX011, math.div(obj.data.GWEA006,100)), math.add(1, math.div(obj.data.GWEA006,100))), vueData.AMOUNT) : '';
                            var MMRX016 = MMRX014 != '' ? self.calculate(math.div(math.mul(MMRX014, math.div(obj.data.GWEA006,100)), math.add(1, math.div(obj.data.GWEA006,100))), vueData.AMOUNT) : '';
                            obj.updateOfEdit({
                                MMRX005: MMRX005,
                                MMRX011: MMRX011,
                                MMRX012: MMRX012,
                                MMRX013: MMRX013,
                                MMRX014: MMRX014,
                                MMRX015: MMRX015,
                                MMRX016: MMRX016
                            });
                            var listData = gwei025Table.getDatas();
                            var sum = 0;
                            for(var i=0;i<listData.length;i++){
                                sum = math.add(sum, listData[i].MMRX005);
                            }
                            vueData.MMRX005_SUM = sum;
                        }else if(obj.field == 'MMRX005'){
                            var MMRX011 = self.calculate(math.mul(obj.value, obj.data.MMRX010), vueData.AMOUNT);
                            var MMRX015 = self.calculate(math.div(math.mul(MMRX011, math.div(obj.data.GWEA006,100)), math.add(1, math.div(obj.data.GWEA006,100))), vueData.AMOUNT);
                            obj.updateOfEdit({
                                MMRX011: MMRX011,
                                MMRX015: MMRX015
                            });
                            var listData = gwei025Table.getDatas();
                            var sum = 0;
                            for(var i=0;i<listData.length;i++){
                                sum = math.add(sum, listData[i].MMRX005);
                            }
                            vueData.MMRX005_SUM = sum;
                        }else if(obj.field == 'MMRX011'){
                            var MMRX015 = self.calculate(math.div(math.mul(obj.value, math.div(obj.data.GWEA006,100)), math.add(1, math.div(obj.data.GWEA006,100))), vueData.AMOUNT);
                            obj.updateOfEdit({
                                MMRX015: MMRX015
                            });
                        }else if(obj.field == 'MMRX012'){//本币单价
                            var MMRX013 = obj.data.MMRX009 != '' ? self.calculate(math.mul(obj.value, obj.data.MMRX003), vueData.AMOUNT) : '';
                            var MMRX014 = obj.data.MMRX009 != '' ? self.calculate(math.mul(MMRX013, obj.data.MMRX010), vueData.AMOUNT) : '';
                            var MMRX016 = self.calculate(math.div(math.mul(MMRX014, math.div(obj.data.GWEA006,100)), math.add(1, math.div(obj.data.GWEA006,100))), vueData.AMOUNT);
                            obj.updateOfEdit({
                                MMRX013: MMRX013,
                                MMRX014: MMRX014,
                                MMRX016: MMRX016
                            });
                        }else if(obj.field == 'MMRX013'){
                            var MMRX014 = obj.data.MMRX009 != '' ? self.calculate(math.mul(obj.value, obj.data.MMRX010), vueData.AMOUNT) : '';
                            var MMRX016 = self.calculate(math.div(math.mul(MMRX014, math.div(obj.data.GWEA006,100)), math.add(1, math.div(obj.data.GWEA006,100))), vueData.AMOUNT);
                            obj.updateOfEdit({
                                MMRX014: MMRX014,
                                MMRX016: MMRX016
                            });
                        }else if(obj.field == 'MMRX014'){
                            var MMRX016 = self.calculate(math.div(math.mul(obj.value, math.div(obj.data.GWEA006,100)), math.add(1, math.div(obj.data.GWEA006,100))), vueData.AMOUNT);
                            obj.updateOfEdit({
                                MMRX016: MMRX016
                            });
                        }else if(obj.field == 'MMRX009'){
                            if(obj.data.MMRX004 != '' && obj.data.MMRX004 != null && obj.data.MMRX004 != undefined){
                                var MMRX012 = self.calculate(math.div(obj.data.MMRX004, obj.value), vueData.AMOUNT);
                                var MMRX013 = self.calculate(math.mul(MMRX012, obj.data.MMRX003), vueData.AMOUNT);
                                var MMRX014 = self.calculate(math.mul(MMRX013, obj.data.MMRX010), vueData.AMOUNT);
                                var MMRX016 = self.calculate(math.div(math.mul(MMRX014, math.div(obj.data.GWEA006,100)), math.add(1, math.div(obj.data.GWEA006,100))), vueData.AMOUNT);
                                obj.updateOfEdit({
                                    MMRX012: MMRX012,
                                    MMRX013: MMRX013,
                                    MMRX014: MMRX014,
                                    MMRX016: MMRX016
                                });
                            }
                        }
                    });
                });
            },
            tableReload: function (listData) {
                self._reloadTable({id: 'gwei025_edit_table', data: listData});
            },

            addData: function (listData) {
                app.ajax({
                    xurl: 'crm/gwei025/addData',
                    xdata: listData,
                    toJsonStr: true,
                    okmsg: moduleName + '增加成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwei025').reloadTableCurr();
                            layer.close(layOpenIndex);
                        }
                    }
                });
            },

            editData: function (listData) {
                app.ajax({
                    xurl: 'crm/gwei025/editData',
                    xdata: listData,
                    toJsonStr: true,
                    okmsg: moduleName + '修改成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwei025').reloadTableCurr();
                            layer.close(layOpenIndex);
                        }
                    }
                });
            },

            _call: function (tagData) {
                moduleName = tagData.moduleName;
                layOpenIndex = tagData.layOpenIndex;
                if (tagData.is_check) {
                    vueData.isCheck = true;
                } else {
                    vueData.isCheck = false;
                }
                if (tagData.is_add) {
                    vueData.isAdd = true;
                    app.ajax({
                        xurl: 'crm/gwei025/getAddData',
                        cb: function (result) {
                            if (result.ok) {
                                app.extendUpLow({
                                    target: vueData.form,
                                    res: result.data.data,
                                    reset: true
                                });
                                var listData = [];
                                self.tableReload(listData);
                            }
                        }
                    });
                } else {
                    vueData.isAdd = false;
                    vueData.isCheck = tagData.is_check;
                    callObj = tagData.obj;
                    app.extendUpLow({
                        target: vueData.form,
                        res: callObj.data,
                        reset: true
                    });
                    var MMRX024_ = callObj.data.MMRX024.split(';');
                    vueData.form.MMRX024_DATA =  '地址:' + MMRX024_[0] + '\n' + '电话:' + MMRX024_[1] + ' ' + '传真:'
                        + MMRX024_[2] +  '\n' + '开户行:' + MMRX024_[3] + '\n' + '账号:' + MMRX024_[4] + '\n'
                        + '税号:' + MMRX024_[5] + '\n';
                    var MMRX025_ = callObj.data.MMRX025.split(';');
                    vueData.form.MMRX025_DATA =  '地址:' + MMRX025_[0] + ' 联系人:' + MMRX025_[1] + ' 联系电话:' + MMRX025_[2];
                    vueData.form.MMRX020 = MMRX025_[1];
                    vueData.form.MMRX021 = MMRX025_[2];
                    vueData.form.MMRX023 = MMRX025_[0];
                    var MMRX026_ = callObj.data.MMRX026.split(';');
                    vueData.form.MMRX026_DATA =  '地址:' + MMRX026_[0] + ' 联系人:' + MMRX026_[1] + ' 联系电话:' + MMRX026_[2];
                    app.ajax({
                        xurl: 'crm/gwei025/getMmrxData',
                        xdata: {MMRXDOCNO: callObj.data.MMRXDOCNO},
                        cb: function (result) {
                            if(result.ok){
                                if(result.data.data.length > 0){
                                    self.tableReload(result.data.data);
                                    /*app.ajax({
                                        xurl: 'crm/gwei025/getMB002',
                                        xdata: {MMRXPLANT: result.data.data[0].MMRXPLANT},
                                        cb: function (result) {
                                            if(result.ok){
                                                if(result.data.data != null){
                                                    vueData.form.MMRXPLANT_NAME = result.data.data.MB002;
                                                }
                                            }
                                        }
                                    });*/
                                }
                            }
                        }
                    });
                    app.ajax({
                        xurl: 'crm/gwei025/getMmrx005Sum',
                        xdata: {MMRXDOCNO: callObj.data.MMRXDOCNO},
                        success: function (result) {
                            if(result.ok) {
                                vueData.MMRX005_SUM = result.data.data.MMRX005_SUM;
                            }
                        }
                    });
                }
                app.ajax({
                    xurl: 'admin/getDigit',
                    cb: function (result) {
                        if(result.ok) {
                            var data = result.data.data;
                            vueData.AMOUNT = Math.pow(10,data.amount);
                        }
                    }
                });
            },

            calculate:function (num,digit) {
                var data = math.div(Math.round(math.mul(num, digit)),digit);
                return data;
            },

            openMmrx018Window:function () {
                self._openTplFull({
                    tag: 'common_open_window',
                    tagData: {
                        config: {
                            method: 'q_gwak001',
                            columnList: [
                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                {label: '客户全称', prop: 'GWAKL004', width: '150'},
                                {label: '手机号码', prop: 'GWAK030', width: '150',sortable:false},
                                {label: '币别', prop: 'GWAK008', width: '150',sortable:false}
                            ],
                            // url: 'crm/gwei025/listGwak'
                        },
                        cb: function (data) {
                            vueData.form.MMRX018 = data.GWAK001;
                            vueData.form.MMRX018_NAME = data.GWAKL004;
                            vueData.MMRX006 = data.GWAK008;
                            vueData.GWAK057 = data.GWAK057;
                            if (data.GWAK004 == 'NULL') {
                                data.GWAK004 = ' ';
                            }
                            app.ajax({
                                xurl: 'crm/gwei025/getGwal',
                                xdata: {GWAL001: data.GWAK001, GWAL003: 'D'},
                                cb: function (result) {
                                    if(result.ok) {
                                        var _data = result.data.data, _arr = [];
                                        _arr.push(_data.GWAL018 || '', _data.GWAL019 || '', _data.GWAL020 || '', _data.GWAL021 || '');
                                        vueData.MMRX025_GWAL018 = (_data.GWAL018 || '');
                                        vueData.MMRX025_GWAL021 = (_data.GWAL021 || '');
                                        vueData.MMRX025_GWAL020 = (_data.GWAL020 || '');
                                        vueData.form.MMRX025_DATA = '地址:' + vueData.MMRX025_GWAL018 + ' 联系人:' + vueData.MMRX025_GWAL021 + ' 联系电话:' + vueData.MMRX025_GWAL020;
                                        vueData.form.MMRX025 = vueData.MMRX025_GWAL018 + ';' + vueData.MMRX025_GWAL021 + ';' + vueData.MMRX025_GWAL020;
                                        vueData.form.MMRX020 = vueData.MMRX025_GWAL021;
                                        vueData.form.MMRX021 = vueData.MMRX025_GWAL020;
                                        vueData.form.MMRX023 = vueData.MMRX025_GWAL018;
                                    }
                                }
                            });
                            app.ajax({
                                xurl: 'crm/gwei025/getGwakData',
                                xdata: {GWAK001: data.GWAK001},
                                cb: function (result) {
                                    if(result.ok) {
                                        var _data = result.data.data;
                                        vueData.form.MMRXLEGAL = _data.GWAKMLEGAL;
                                        vueData.form.MMRXLEGAL_NAME = _data.GWAKMLEGAL_NAME;
                                        vueData.form.MMRXPLANT = _data.GWAKPLANT;

                                    }
                                }
                            });
                        }
                    },
                    index: 1,
                    openOpts: {
                        title: '选择最终用户信息',
                        area: ['600px', '470px']
                    }
                });
            },

            openMmrx019Window:function () {
                self._openTplFull({
                    tag: 'common_open_window',
                    tagData: {
                        config: {
                            columnList: [
                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                            ],
                            url: 'crm/gwei025/getMmrx019'
                        },
                        cb: function (data) {
                            vueData.form.MMRX019 = data.GWAK001;
                            vueData.form.MMRX019_NAME = data.GWAKL004;
                            vueData.form.MMRX024_DATA = '地址:' + (data.GWAK028 || '') + '\n' + '电话:'
                                + (data.GWAK013 || '') + ' ' + '传真:' + (data.GWAK014 || '') + '\n'
                                + '开户行:' + (data.GWAK004 || '') + '\n' + '账号:' + (data.GWAK003 || '') + '\n'
                                + '税号:' + (data.GWAK007 || '') + '\n';
                            vueData.form.MMRX024 = (data.GWAK028 || '')+ ';' + (data.GWAK013 || '')+';' + (data.GWAK014 || '') + ';'
                                + (data.GWAK004 || '') + ';' + (data.GWAK003 || '') + ';' + (data.GWAK007 || '');
                            app.ajax({
                                xurl: 'crm/gwei025/getGwal',
                                xdata: {GWAL001: data.GWAK001, GWAL003: 'I'},
                                cb: function (result) {
                                    if(result.ok) {
                                        var _data = result.data.data, _arr = [];
                                        _arr.push(_data.GWAL018 || '', _data.GWAL019 || '', _data.GWAL020 || '', _data.GWAL021 || '');
                                        vueData.MMRX026_GWAL018 = (_data.GWAL018 || '');
                                        vueData.MMRX026_GWAL021 = (_data.GWAL021 || '');
                                        vueData.MMRX026_GWAL020 = (_data.GWAL020 || '');
                                        vueData.form.MMRX026_DATA = '地址:' + vueData.MMRX026_GWAL018 + ' 联系人:' + vueData.MMRX026_GWAL021 + ' 联系电话:' + vueData.MMRX026_GWAL020;
                                        vueData.form.MMRX026 = vueData.MMRX026_GWAL018 + ';' + vueData.MMRX026_GWAL021 + ';' + vueData.MMRX026_GWAL020;
                                    }
                                }
                            });
                        }
                    },
                    index: 1,
                    openOpts: {
                        title: '选择最终用户信息',
                        area: ['600px', '510px']
                    }
                });
            },

            _ready: function () {
                table = layui.table;
                vueData.form = app.fields_(['MMRX_T'],vueData.form);
                var vue = new Vue({
                    el: '#gwei025_edit',
                    data: vueData,
                    watch: {

                    },
                    methods: {
                        addData: function () {
                            if (vueData.form.MMRX018 == '' || vueData.form.MMRX018 == null) {
                                app.showWarning("请先选择用户！");
                                return;
                            }
                            if (vueData.form.MMRX019 == '' || vueData.form.MMRX019 == null) {
                                app.showWarning("请先选择签约单位！");
                                return;
                            }
                            if (vueData.form.MMRX032 == '' || vueData.form.MMRX032 == null) {
                                app.showWarning("请先选择销售部门！");
                                return;
                            }
                            app.ajax({
                                async:false,
                                xurl: 'crm/gwei025/getHuilvZhekou',
                                xdata: {GWAK001: vueData.form.MMRX018},//通过经销商获取
                                cb: function (result) {
                                    if(result.ok) {
                                        var data = result.data.data;
                                        vueData.GWEA006 = data.GWEA006;
                                        vueData.MMRX010 = data.MMRX010; //折扣率
                                        vueData.MMRX009 = data.MMRX009; //汇率
                                        vueData.MMRX006 = data.MMRX006; //币别
                                        vueData.MMRX006_name = data.MMRX006_name; //币别
                                        vueData.MMRX007_name = data.MMRX007_name; //税种
                                        vueData.MMRX007 = data.MMRX007; //税种
                                    }
                                }
                            });
                            var choose_data = gwei025Table.getDatas();
                            /*var count = 0;
                            if(choose_data.length > 0) {
                                count = choose_data[choose_data.length - 1].MMRWSEQ;
                            }
                            choose_data.push({
                                MMRWSEQ: count + 1,
                                MMRW001: '',
                                GWAML003: '',
                                GWAML004: '',
                                MMRW002: '',
                                MMRW003: '',
                                MMRW005: '',
                                MMRW015: '',
                                MMRW011: '', //报工单号
                                MMRW012: '', //报工单项次
                                MMRX010: vueData.MMRX010 //折扣率
                            });
                            self.tableReload(choose_data);*/
                            var str_data = [];
                            choose_data.forEach(function (v) {
                                var str = v.MMRX027 + v.MMRX028;
                                str_data.push(str);
                            });
                            var count = 0;
                            if(choose_data.length > 0) {
                                count = choose_data[choose_data.length - 1].MMRXSEQ;
                            }
                            self._openTplFull({
                                tag: 'common_open_multi_mmrghi',
                                tagData: {
                                    operation: 'add',
                                    STR: str_data.join(','),
                                    MMRX032: vueData.form.MMRX032,
                                    MMRX018: vueData.form.MMRX018,
                                    cb: function (data) {
                                        var sum = 0;
                                        for(var i = 0; i< data.length; i++){
                                            var MMRX011 = self.calculate(math.mul(data[i]["MMRH006"] , vueData.MMRX010), vueData.AMOUNT);
                                            var MMRX014 = self.calculate(math.mul(math.mul( data[i]["MMRH006"], vueData.MMRX010), vueData.MMRX009), vueData.AMOUNT);
                                            var MMRX015 = self.calculate(math.div(MMRX011, math.mul(math.add(1, math.div(vueData.GWEA006, 100)), math.div(vueData.GWEA006, 100))),vueData.AMOUNT);
                                            var MMRX016 = self.calculate(math.div(MMRX014, math.mul(math.add(1, math.div(vueData.GWEA006, 100)), math.div(vueData.GWEA006, 100))),vueData.AMOUNT);
                                            choose_data.push({
                                                MMRXSEQ: count + 1 + i,
                                                MMRX001: data[i]["MMRH003"],
                                                GWAML003: data[i]["GWAML003"],
                                                MMRCL003: data[i]["MMRCL003"],
                                                GWAML004: data[i]["GWAML004"],
                                                MMRX002: data[i]["GWAM008"],
                                                MMRX003: data[i]["MMRH004"],
                                                MMRX004: data[i]["MMRH005"],
                                                MMRX005: data[i]["MMRH006"],
                                                MMRX006: vueData.MMRX006,
                                                MMRX007: vueData.MMRX007,
                                                MMRX007_NAME: vueData.MMRX007_NAME,
                                                MMRX009: vueData.MMRX009,
                                                MMRX010: vueData.MMRX010,
                                                MMRX011: self.calculate(math.mul(vueData.MMRX010, data[i]["MMRH006"]), vueData.AMOUNT),
                                                MMRX012: self.calculate(math.mul(vueData.MMRX009, data[i]["MMRH005"]), vueData.AMOUNT),
                                                MMRX013: self.calculate(math.mul(vueData.MMRX009, data[i]["MMRH006"]), vueData.AMOUNT),
                                                MMRX014: self.calculate(math.mul(math.mul(vueData.MMRX010, data[i]["MMRH006"]), vueData.MMRX009), vueData.AMOUNT),
                                                MMRW015: MMRX015,
                                                MMRW016: MMRX016,
                                                MMRW027: data[i]["MMRHDOCNO"],
                                                MMRW028: data[i]["MMRHSEQ"],
                                                MMRW031: "",
                                                MMRW011: data[i]["MMRHDOCNO"], //报工单号
                                                MMRW012: data[i]["MMRHSEQ"] //报工单项次
                                            });
                                            sum = math.add(sum, data[i]["MMRH006"]);
                                        }
                                        vueData.MMRX005_SUM = math.add(vueData.MMRX005_SUM, sum);
                                        self.tableReload(choose_data);
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择报工单信息',
                                    area: ['1000px', '470px']
                                }
                            });
                        },
                        cancle: function () {
                            layer.close(layOpenIndex);
                        },
                        delData: function () {
                            listData = gwei025Table.getDatas();
                            var checkStatus = table.checkStatus('gwei025_edit_table');
                            if (checkStatus.data.length > 0) {
                                checkStatus.data.forEach(function (v) {
                                    listData.removeByMap({
                                        MMRXSEQ: v.MMRXSEQ
                                    });
                                });
                                var sum = 0;
                                for(var i=0; i<listData.length;i++){
                                    sum = math.add(sum, listData[i].MMRX005);
                                }
                                vueData.MMRX005_SUM = sum;
                                self.tableReload(listData);
                            }
                        },
                        saveData: function () {
                            this.$refs['form'].validate(function (valid) {
                                if (valid) {
                                    var flag = true;
                                    var listData = gwei025Table.getDatas();
                                    if(listData.length < 1){
                                        app.showWarning('表格数据不能为空！');
                                        return;
                                    }
                                    for (var i = 0; i < listData.length; i++) {
                                        if(listData[i].MMRX003 <= 0){
                                            app.showWarning('数量必须大于0！');
                                            flag = false;
                                            return;
                                        }
                                        if(listData[i].MMRX007 == null || listData[i].MMRX007 === '') {
                                            app.showWarning('税种不可为空');
                                            flag = false;
                                            return;
                                        }
                                        if(listData[i].MMRX006 == null || listData[i].MMRX006 === '') {
                                            app.showWarning('币别为空，请检查客户资料');
                                            flag = false;
                                            return;
                                        }
                                        if(listData[i].MMRX009 == null || listData[i].MMRX009 === '') {  // 汇率
                                            vueData.form.MMRX009 = 1;
                                            return;
                                        }
                                    }
                                    listData.addMap(vueData.form, false);
                                    if(flag){
                                        if (vueData.isAdd) {
                                            self.addData(listData);
                                        } else {
                                            self.editData(listData);
                                        }
                                    }

                                }
                            });
                        },
                        openMmrx018Window:function () {//最终用户
                            if(vueData.form.MMRX018 != '' && vueData.form.MMRX018 != null){
                                var count = gwei025Table.getDatas().length;
                                if(count.length != 0){
                                    app.confirm({msg: "是否确认修改最终用户？",
                                        ok:function () {
                                            var listData = [];
                                            self.tableReload(listData);
                                            self.openMmrx018Window();
                                        }
                                    });
                                }else {
                                    self.openMmrx018Window();
                                }
                            }else {
                                self.openMmrx018Window();
                            }
                        },
                        openMmrxlegalWindow:function () {//归属法人开窗
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'cq_sysorg_biz_2',
                                        columnList: [
                                            {label: 'ID', prop: 'ID', width: '150'},
                                            {label: '归属法人', prop: 'NAME', width: '150'},
                                            {label: '营运中心编号', prop: 'BIZ_CODE', width: '150'},
                                            //{label: '营运中心名称', prop: 'BIZ_CODE_NAME', width: '150'}
                                        ],
                                    },
                                    cb: function (data) {
                                        vueData.form.MMRXLEGAL_NAME = data.NAME;
                                        vueData.form.MMRXLEGAL = data.ID;
                                        vueData.form.MMRXPLANT = data.BIZ_CODE;
                                        //vueData.form.MMRXPLANT_NAME = data.BIZ_CODE_NAME;
                                    }
                                },
                                index: 1,
                                openOpts: {
                                    title: '选择归属法人信息',
                                    area: ['600px', '470px']
                                }
                            });
                        },
                        clearMmrxlegal:function() {
                            vueData.form.MMRXLEGAL_NAME = '';
                            vueData.form.MMRXLEGAL = '';
                            vueData.form.MMRXPLANT = '';
                        },
                        openMmrx019Window:function () {//签约单位开窗
                            if(vueData.form.MMRX019 != '' && vueData.form.MMRX019 != null){
                                var count = gwei025Table.getDatas().length;
                                if(count.length != 0){
                                    app.confirm({msg: "是否确认修改签约单位？",
                                        ok:function () {
                                            var listData = [];
                                            self.tableReload(listData);
                                            self.openMmrx019Window();
                                        }
                                    });
                                }else {
                                    self.openMmrx019Window();
                                }
                            }else {
                                self.openMmrx019Window();
                            }
                        },
                        openMmrx022Window:function () {//销售人员开窗
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_sysuser_id',
                                        columnList: [
                                            {label: 'ID', prop: 'ID', width: '150'},
                                            {label: '账号', prop: 'ACCOUNT', width: '150'},
                                            {label: '姓名', prop: 'NAME', width: '150'}
                                        ],
                                        // url: 'crm/gwei025/getUserData'
                                    },
                                    cb: function (data) {
                                        vueData.form.MMRX022 = data.ID;
                                        vueData.form.MMRX022_NAME = data.NAME;
                                        app.ajax({
                                            xurl: 'crm/gwei025/getOrg',
                                            xdata: {ID:vueData.form.MMRX022},
                                            cb: function (result) {
                                                if(result.ok) {
                                                    vueData.form.MMRXPLANT = result.data.data.list.BIZ_CODE;
                                                    vueData.form.MMRXPLANT_NAME = result.data.data.list.BIZ_CODE_NAME;
                                                    vueData.form.MMRXLEGAL = result.data.data.ID;
                                                    vueData.form.MMRXLEGAL_NAME = result.data.data.NAME;
                                                }
                                            }
                                        });
                                    }
                                },
                                index: 1,
                                openOpts: {
                                    title: '选择销售人员信息',
                                    area: ['600px', '470px']
                                }
                            });
                        },
                        openMmrx032Window:function () {//销售部门开窗
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_sysorg_id',
                                        columnList: [
                                            {label: '部门ID', prop: 'ID', width: '150'},
                                            {label: '部门名称', prop: 'NAME', width: '150'}
                                        ],
                                        // url: 'crm/gwei025/getOrgData'
                                    },
                                    cb: function (data) {
                                        vueData.form.MMRX032 = data.ID;
                                        vueData.form.MMRX032_NAME = data.NAME;
                                    }
                                },
                                index: 1,
                                openOpts: {
                                    title: '选择销售部门信息',
                                    area: ['600px', '470px']
                                }
                            });
                        },
                        openGWAK:function () {//修改开票
                            if (vueData.form.MMRX019 == '' || vueData.form.MMRX019 == null) {
                                app.showWarning("请先选择签约单位！");
                                return ;
                            }
                            self._openTplFull({
                                tag: 'gwei025_edit_gwak',
                                tagData: {
                                    GWAK001: vueData.form.MMRX019,
                                    cb: function (data) {
                                        vueData.form.MMRX024_DATA = '地址:' + (data.GWAK028 || '') + '\n' + '电话:'
                                            + (data.GWAK013 || '') + ' ' + '传真:' + (data.GWAK014 || '') + '\n'
                                            + '开户行:' + (data.GWAK004 || '') + '\n' + '账号:' + (data.GWAK003 || '') + '\n'
                                            + '税号:' + (data.GWAK007 || '') + '\n';
                                        vueData.form.MMRX024 = (data.GWAK028 || '') + ';' + (data.GWAK013 || '') + ';'
                                            + (data.GWAK014 || '') + ';' + (data.GWAK004 || '') + ';' + (data.GWAK003 || '') + ';'
                                            + (data.GWAK007 || '');
                                    }
                                },
                                index: 2,
                                openOpts: {
                                    title: '修改开票信息',
                                    area: ['800px', '350px']
                                }
                            });
                        },
                        openMmrx025:function () {//修改送货
                            if (vueData.form.MMRX018 == '' || vueData.form.MMRX018 == null) {
                                app.showWarning("请先选择最终用户！");
                                return ;
                            }
                            self._openTplFull({
                                tag: 'gwei025_edit_gwal',
                                tagData: {
                                    operation: 'D',
                                    data:{
                                        GWAK001: vueData.form.MMRX018,
                                        GWAKL003: vueData.form.MMRX018_NAME,
                                        GWAL018: vueData.MMRX025_GWAL018,
                                        GWAL020: vueData.MMRX025_GWAL020,
                                        GWAL021: vueData.MMRX025_GWAL021,
                                        GWAL003: 'D'
                                    },
                                    cb: function (data) {
                                        vueData.MMRX025_GWAL018 = data.GWAL018;
                                        vueData.MMRX025_GWAL021 = data.GWAL021;
                                        vueData.MMRX025_GWAL020 = data.GWAL020;
                                        vueData.form.MMRX025_DATA = '地址:' + data.GWAL018 + ' 联系人:' +  data.GWAL021 + ' 联系电话:' + data.GWAL020;
                                        vueData.form.MMRX025 = data.GWAL018 + ';' +  data.GWAL021 + ';' + data.GWAL020;
                                        //vueData.form.MMRX025 = vueData.MMRX025_GWAL018 + ';' + vueData.MMRX025_GWAL021 + ';' + vueData.MMRX025_GWAL020;
                                        vueData.form.MMRX020 = data.GWAL021;
                                        vueData.form.MMRX021 = data.GWAL020;
                                        vueData.form.MMRX023 = data.GWAL018;
                                    }
                                },
                                index: 3,
                                openOpts: {
                                    title: '修改开票信息',
                                    area: ['600px', '310px']
                                }
                            });
                        },
                        openMmrx026:function () {//修改发票
                            if (vueData.form.MMRX019 == '' || vueData.form.MMRX019 == null) {
                                app.showWarning("请先选择签约单位！");
                                return ;
                            }
                            self._openTplFull({
                                tag: 'gwei025_edit_gwal',
                                tagData: {
                                    operation: 'I',
                                    data:{
                                        GWAK001: vueData.form.MMRX019,
                                        GWAKL003: vueData.form.MMRX019_NAME,
                                        GWAL018: vueData.MMRX026_GWAL018,
                                        GWAL020: vueData.MMRX026_GWAL020,
                                        GWAL021: vueData.MMRX026_GWAL021,
                                        GWAL003: 'I'
                                    },
                                    cb: function (data) {
                                        vueData.MMRX026_GWAL018 = data.GWAL018;
                                        vueData.MMRX026_GWAL021 = data.GWAL021;
                                        vueData.MMRX026_GWAL020 = data.GWAL020;
                                        vueData.form.MMRX026_DATA = '地址:' + data.GWAL018 + ' 联系人:' +  data.GWAL021 + ' 联系电话:' + data.GWAL020;
                                        vueData.form.MMRX026 = data.GWAL018 + ';' +  data.GWAL021 + ';' + data.GWAL020;
                                    }
                                },
                                index: 3,
                                openOpts: {
                                    title: '修改发票信息',
                                    area: ['600px', '310px']
                                }
                            });
                        }

                    },
                    created: function () {
                        self._setEleIsReady('gwei025_edit');
                    }
                });
                self.initTable();
            },
            _lazy: function () {
                this._super();
            }
        });

    return new Gwei025Edit();

});